/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.glc.hms.core.shiro.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glc.hms.core.common.ConstantFactory;
import com.glc.hms.core.shiro.ShiroUser;
import com.glc.hms.core.shiro.ShiroUtil;
import com.glc.hms.core.shiro.service.UserAuthService;
import com.glc.hms.holder.SpringContextHolder;
import com.glc.hms.system.dao.SysUserMapper;
import com.glc.hms.system.entity.SysRole;
import com.glc.hms.system.entity.SysUser;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Santa
 * @since 2019/11/14
 */
@Service
@DependsOn("springContextHolder")
//@Transactional(readOnly = true)
public class UserAuthServiceImpl implements UserAuthService {

    @Autowired
    private SysUserMapper userMapper;




    public static UserAuthService me() {
        return SpringContextHolder.getBean(UserAuthService.class);
    }

    @Override
    public SysUser user(String account) {

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",account);
        List<SysUser> bUsers = userMapper.selectList(queryWrapper);

        // 账号不存在
        if (bUsers.size()<1) {
            throw new CredentialsException();
        }
        // 账号被冻结
        if (bUsers.get(0).getEnabled().equals(0)) {
            throw new LockedAccountException();
        }
        SysUser user = bUsers.get(0);
        user.setLastLoginTime(new Date());
        userMapper.updateById(user);
        return user;
    }

    @Override
    public ShiroUser shiroUser(SysUser user) {

        ShiroUser shiroUser = ShiroUtil.createNewShiroUser(user);

        //查询角色信息
        Long userId = shiroUser.getUserId();
        SysRole role = ConstantFactory.me().getRoleByUserId(userId);
        if (role != null) {
            shiroUser.setRoleId(role.getRoleId());
            shiroUser.setRoleName(role.getRoleName());
        }

        //查询角色部门信息
        String deptName = ConstantFactory.me().getDeptNameById(shiroUser.getDeptId());
        shiroUser.setDeptName(deptName);

        return shiroUser;
    }




    @Override
    public SimpleAuthenticationInfo info(ShiroUser shiroUser, SysUser user, String realmName) {
        String credentials = user.getPassword();
        // 密码加盐处理
        String source = ShiroUtil.glc_ADLT;
        ByteSource credentialsSalt = new Md5Hash(source);
        return new SimpleAuthenticationInfo(shiroUser, credentials, credentialsSalt, realmName);
    }

}
